package odgs.app.client;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JTextArea;

import odgs.Connection;
import odgs.Explorer;
import odgs.ExplorerNode;
import odgs.GridState;
import odgs.Transaction;
import odgs.listener.DataManagerListener;
import odgs.listener.ExplorerListener;
import odgs.listener.TransactionManagerListener;
import odgs.manager.DataManager;
import odgs.manager.TransactionManager;

public class CliectLogViewController implements DataManagerListener, TransactionManagerListener, ExplorerListener {

	private ClientMainFrameUI ui;
	private JTextArea viewer;
	private DataManager dataManager;
	private String log;
	private String log2;

	public CliectLogViewController(ClientMainFrameUI ui) {
		this.ui = ui;
		ui.log.setAutoscrolls(true);
		viewer = ui.log.view;
		viewer.setLineWrap(true);
		viewer.setEditable(false);
	}

	public void setDataManager(DataManager dataManager) {
		this.dataManager = dataManager;
		dataManager.addDataManagerListener(this);
		log("Log Start!");
	}

	public void error(int error) {
		viewerlog("Error Code = " + error);
	}

	public void explorerChanged(Explorer explorer) {}

	public void joined(GridState state, Explorer explorer, String id) {
		viewerlog("join \"" + id + "\"");
	}

	public void logined(GridState state, Explorer explorer) {
		viewerlog("logined OK");
	}

	public void serverConnected(Connection con) {
		viewerlog("Server Connected OK");
	}

	public void stateChanged(GridState state) {}

	public void added(Transaction tran) {
		viewerlog(tran.getRequestId() + " : " + tran.getName() + " started");
	}

	public void done(Transaction tran) {
		viewerlog(tran.getRequestId() + " : " + tran.getName() + " done");
	}

	public void error(int error, Transaction tran) {
		viewerlog(tran.getRequestId() + " : "  + tran.getName() + " is failed");
	}

	private void viewerlog(String log) {
		log(" Logic Log : " + log);
	}
	
	public void log(String log) {
		viewer.append("  (" + getTime() + ") " + log + "\n\r");
		viewer.setCaretPosition(viewer.getDocument().getLength());
	}

	private String getTime() {
		SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy hh:mm:ss");
		return format.format(new Date());
	}

	public void added(ExplorerNode parent, ExplorerNode node) {
		if(node.canChildren())
			viewerlog("Added Directory " + node.getOwner() + "'s \"" + node.getPath() + node.toString() + "\"");
		else
			viewerlog("Added File " + node.getOwner() + "'s \"" + node.getPath() + node.toString() + "\"");
	}

	public void modified(ExplorerNode node) {
		viewerlog("changed " + node.getOwner() + "'s \"" + node.getPath() + node + "\"");
	}

	public void moved(ExplorerNode node) {
		
	}

	public void removed(ExplorerNode parent, ExplorerNode node) {
		if(parent.canChildren())
			viewerlog("Removed Directory " + node.getOwner() + "'s \"" + node.getPath() + node.toString() + "\"");
		else
			viewerlog("Removed File " + node.getOwner() + "'s \"" + node.getPath() + node.toString() + "\"");
	}

	public void setedPosition(String position) {}
	public void copied(ExplorerNode node) {}

	public void setTransactionManager(TransactionManager transManager) {
		transManager.addTransactionManagerListener(this);
	}

	public void setExplorer(Explorer explorer) {
		explorer.addExplorerModifyListener(this);
	}

}
